<?php
class Paycallback extends BZ_Controller
{
    
    protected $key;
    protected $iv;
    public function __construct()
    {
        parent::__construct();
        $this->key = 'd77ab88a8400ed08'; //aes 密钥
        $this->iv = '6c031d737fba7fcf'; //aes 偏移量
        $this->load->library('xcrypt',null,'xcrypt');// 加密类
        $this->load->model('pay_failure_log_model', 'pay_failure_log');
    }
    
    /**
     * 微信jsapi支付回调
    */
    public function wxNotify()
    {  
        include_once(getcwd()."/application/libraries/wxpay/WxPayPubHelper/WxPayPubHelper.php");
        $notify = new Notify_pub(); //使用通用通知接口
        $xml    = file_get_contents('php://input', 'r');  //存储微信的回调
        $notify->saveData($xml);

        //验证签名，并回应微信。
        //对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
        //微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
        //尽可能提高通知的成功率，但微信不保证通知最终能成功。
        if( $notify->checkSign() == FALSE ){
            $notify->setReturnParameter("return_code","FAIL");  //返回状态码
            $notify->setReturnParameter("return_msg","签名失败");  //返回信息
        }else{
            $notify->setReturnParameter("return_code","SUCCESS");//设置返回码
        }
        $returnXml = $notify->returnXml();
        echo $returnXml;      //echo为微信返回值   不能更改   不能删除

        if($notify->checkSign() == TRUE)
        {
            if ($notify->data["return_code"] == "FAIL" || $notify->data["result_code"] == "FAIL")
            {
                $return_param = "【通信出错】【业务出错】:\n".$xml."\n";
                $last_id = $this->pay_failure_log->insertPayFailureLog($notify->data["out_trade_no"], $return_param, "酒店微信支付报错");
            }else{
                $order_sn = $notify->data["out_trade_no"];
                $this->xcrypt->init($this->key, 'cbc', $this->iv);
                $order_id= $this->xcrypt->encrypt($order_sn,'hex');  //进行加密
                $url = $this->config->hotel_api_url.'m/payment/wxNotify';
                $param = array(
                	'order_sn' => $order_id,
                );
                $result = json_decode(fn_get_contents($url, $param, 'post'));
                if (empty($result)) {
                    $this->pay_failure_log->insertPayFailureLog($order_sn, '调用接口返回参数为空值', "酒店微信支付调api报错");
                }
                if (!empty($result->status)) {
                    $this->pay_failure_log->insertPayFailureLog($order_sn, $result->status, "酒店微信支付调api报错");
                }
            }
        }
    }
}

